<template>
  <div>
    <!-- 添加或修改资产清单表对话框 -->
    <el-dialog v-model="openFlag" :title="title" width="600" append-to-body>

      <vxe-table style="height: 100%;flex-grow: 1;"
                 border="full"
                 ref="tableRef"
                 :column-config="{resizable: true}"
                 :checkbox-config="{checkField: 'checked'}"
                 :data="list">
        <vxe-column field="roleName" title="名称"></vxe-column>
        <vxe-column field="roleKey" title="编码"></vxe-column>
        <vxe-column type="checkbox" title="" field="checked" width="280"></vxe-column>
      </vxe-table>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script setup>

import api from "./api.js";

const { proxy } = getCurrentInstance();

const emit = defineEmits(['sub'])

const openFlag = ref(false);
const title = ref();
const tableRef = ref();
let userId = null;

const list = ref([]);

function getList() {
  return api.listRole(userId)
}

function open(row) {
  userId = row.userId;
  title.value = `用户：【${row.nickName}】的角色`;
  openFlag.value = true;
  getList().then(response => {
    list.value = response;
  })
}

function close() {
  openFlag.value = false;
}

function submitForm() {
  const checkedList = tableRef.value.getCheckboxRecords();
  const roleIdList = checkedList.map( (item)=> {
    return item.roleId;
  });
  api.updateRole(userId, roleIdList).then(response => {
    close();
    emit('sub')
  })
}

defineExpose({
  open
})




</script>

